


import 'reflect-metadata'
export function Get(path: string = ''): MethodDecorator {
  /*
  * @param target 类的原型 
  * @param propertyKey  方法名
  * @param descriptor  方法的属性描述其
  **/
  return (
    target: any,
    propertyKey: string | symbol,
    descriptor: TypedPropertyDescriptor<any>
  ): void => {
    // 给descriptor.value 也就是 hello 函数添加元数据，path=path
    Reflect.defineMetadata('path', path, descriptor.value);
    Reflect.defineMetadata('method', 'GET', descriptor.value);
    //上面设置元数据===给该函数添加属性
    // 这是这样直接在对象函数中添加属性会污染代码
    // descriptor.value.method = 'GET'
  };
}

export function Post(path: string = ''): MethodDecorator {
  /*
  * @param target 类的原型 
  * @param propertyKey  方法名
  * @param descriptor  方法的属性描述其
  **/
  return (
    target: any,
    propertyKey: string | symbol,
    descriptor: TypedPropertyDescriptor<any>
  ): void => {
    // 给descriptor.value 也就是 hello 函数添加元数据，path=path
    Reflect.defineMetadata('path', path, descriptor.value);
    Reflect.defineMetadata('method', 'POST', descriptor.value);
    //上面设置元数据===给该函数添加属性
    // 这是这样直接在对象函数中添加属性会污染代码
    // descriptor.value.method = 'GET'
  };
}

export function Redirect(path: string = '', statusCode: number = 302): MethodDecorator {
  return (
    target: any,
    propertyKey: string | symbol,
    descriptor: TypedPropertyDescriptor<any>
  ): void => {
    Reflect.defineMetadata('redirectUrl', path, descriptor.value);
    Reflect.defineMetadata('redirectStatusCode', statusCode, descriptor.value);
  };
}

export function HttpCode(statusCode: number = 302): MethodDecorator {
  return (
    target: any,
    propertyKey: string | symbol,
    descriptor: TypedPropertyDescriptor<any>
  ): void => {
    Reflect.defineMetadata('statusCode', statusCode, descriptor.value);
  };
}

export function Header(name: string, value: string): MethodDecorator {
  return (
    target: any,
    propertyKey: string | symbol,
    descriptor: TypedPropertyDescriptor<any>
  ): void => {
    let headers = Reflect.getMetadata('headers', descriptor.value) ?? [];
    headers.push({ name, value })
    Reflect.defineMetadata('headers', headers, descriptor.value);
  };
}
